﻿//-----------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls.Primitives;

namespace Microsoft.LibraryManagerDemo.Controls
{
    /// <summary>
    /// Extends Button with an ImageSource and a Text property to create
    /// toolbar buttons with a standardized appearance
    /// </summary>
    public class ToolBarButton : ToggleButton
    {
        public ToolBarButton()
        {
            this.Checked += new RoutedEventHandler(ToolBarButton_Checked);
        }

        void ToolBarButton_Checked(object sender, RoutedEventArgs e)
        {
            if (!this.IsCheckable)
            {
                this.IsChecked = false;
                return;
            }

            // otherwise, we behave like a RadioButton and uncheck everything
            // with a shared parent

            UIElement parent = this;
            Panel parentPanel = null;

            while (true) 
            {
                parent = VisualTreeHelper.GetParent(parent) as UIElement;
                if (parent == null) break;

                parentPanel = parent as Panel;
                if (parentPanel != null) break;
            }

            if (parentPanel != null)
            {
                foreach (UIElement child in parentPanel.Children)
                {
                    ToolBarButton childButton = child as ToolBarButton;
                    if (childButton != null && childButton != this && childButton.IsCheckable) childButton.IsChecked = false; 
                }
            }
        }

        #region Text

        /// <summary>
        /// Text Dependency Property
        /// </summary>
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(ToolBarButton),
                new FrameworkPropertyMetadata((string)null));

        /// <summary>
        /// Gets or sets the Text property.  This dependency property 
        /// indicates the text to be displayed on the ToolBar.
        /// </summary>
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        #endregion

        #region ImageSource

        /// <summary>
        /// ImageSource Dependency Property
        /// </summary>
        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ToolBarButton),
                new FrameworkPropertyMetadata((ImageSource)null));

        /// <summary>
        /// Gets or sets the ImageSource property.  This dependency property 
        /// indicates the image to be used on the ToolBar.
        /// </summary>
        public ImageSource ImageSource
        {
            get { return (ImageSource)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        #endregion

        #region IsDividerVisible

        /// <summary>
        /// IsDividerVisible Dependency Property
        /// </summary>
        public static readonly DependencyProperty IsDividerVisibleProperty =
            DependencyProperty.Register("IsDividerVisible", typeof(bool), typeof(ToolBarButton),
                new FrameworkPropertyMetadata((bool)true));

        /// <summary>
        /// Gets or sets the IsDividerVisible property.  This dependency property 
        /// indicates whether the divider between button should be visible (template only property).
        /// </summary>
        public bool IsDividerVisible
        {
            get { return (bool)GetValue(IsDividerVisibleProperty); }
            set { SetValue(IsDividerVisibleProperty, value); }
        }

        #endregion

        #region IsCheckable

        /// <summary>
        /// IsCheckable Dependency Property
        /// </summary>
        public static readonly DependencyProperty IsCheckableProperty =
            DependencyProperty.Register("IsCheckable", typeof(bool), typeof(ToolBarButton),
                new FrameworkPropertyMetadata((bool)false));

        /// <summary>
        /// Gets or sets the IsCheckable property.  This dependency property 
        /// indicates ....
        /// </summary>
        public bool IsCheckable
        {
            get { return (bool)GetValue(IsCheckableProperty); }
            set { SetValue(IsCheckableProperty, value); }
        }

        #endregion

        



    }
}
